package com.hospital.tools;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.SAXParserFactory;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xmlpull.v1.XmlPullParserException;
import com.hospital.xml.MedSaxHandler;

import android.util.Log;

public class MedArrayListTool {

	private String targetNameSpace = "http://tempuri.org/";
	private String WSDL = "http://www.kingtsoft.com:7001/EsbBusService.asmx?wsdl";
	//private String WSDL = "http://10.1.51.248:77/EsbBusService.asmx?wsdl";
	public List<HashMap<String, String>> getList(String xml,
			HashMap<String, String> in) {
		InputStream ret = getXmlStream(xml, "Process");
		List<HashMap<String, String>> entrys = null;
		entrys = new ArrayList<HashMap<String, String>>(0);
		entrys.add(in);
		parse(ret, entrys);
		return entrys;
	}

	public List<HashMap<String, String>> getXMLParse(String xml,
			HashMap<String, String> in) {

		InputStream ret = new ByteArrayInputStream(xml.getBytes());
		List<HashMap<String, String>> entrys = null;
		entrys = new ArrayList<HashMap<String, String>>(0);
		entrys.add(in);
		parse(ret, entrys);
		return entrys;

	}

	public void parse(InputStream ret, List<HashMap<String, String>> entrys) {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try {
			XMLReader reader = factory.newSAXParser().getXMLReader();
			reader.setContentHandler(new MedSaxHandler(entrys));
			reader.parse(new InputSource(ret));
		} catch (Exception e) {
		}
	}

	public String getXML(String xml, String name) {
		String ret = "";
		SoapObject soapObject = new SoapObject(targetNameSpace, name);
		soapObject.addProperty("XmlString", xml);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soapObject);
		// envelope.bodyOut = soapObject;
		HttpTransportSE httpTranstation = new HttpTransportSE(WSDL);
		try {
			//httpTranstation.call(null, envelope);
			httpTranstation.call(targetNameSpace + name, envelope);
			ret = envelope.getResponse().toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//String xx = e.getMessage();
			//String a =xx;
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.i("xml", ret);
		// 测试数据
		//ret = "<?xml version='1.0' encoding='utf-8' ?> <string xmlns='http://tempuri.org/'><body><response><ret_code>0</ret_code><ret_info>成功</ret_info><MEDICINE_LIST> <MEDICINE> <YPBH>1</YPBH><YPMC>六味地黄丸</YPMC><YPMCSP>l</YPMCSP> <YPTYMC>地黄丸</YPTYMC> <YPYWMC>dihuangwan</YPYWMC></MEDICINE><MEDICINE> <YPBH>2</YPBH><YPMC>白加黑感冒药</YPMC> <YPMCSP>b</YPMCSP><YPTYMC>白加黑</YPTYMC><YPYWMC>baijiahei</YPYWMC></MEDICINE></MEDICINE_LIST></response></body></string>";
		return ret;
	}

	public InputStream getXmlStream(String xml, String name) {
		ByteArrayInputStream stream = new ByteArrayInputStream(
				getXML(xml, name).getBytes());
		return stream;
	}

}
